package com.clearliang.nativeapp.plugins;

import android.content.Context;
import android.util.Log;

import io.flutter.plugin.common.EventChannel;
import io.flutter.view.FlutterView;

/**
 * Created by ClearLiang on 2019/6/4
 * Description：
 * Function :
 */
public class EventPlugin implements EventChannel.StreamHandler {
    /**
     * 通道名称、函数名、参数名
     */
    public final static String CHANNEL = "com.liang.flutter.plugin/event";


    static EventChannel channel;

    private Context context;

    public EventPlugin(Context context) {
        this.context = context;
    }

    public static void registerWith(FlutterView flutterView) {
        channel = new EventChannel(flutterView, CHANNEL);
        EventPlugin flutterPluginEventTest = new EventPlugin(flutterView.getContext());
        channel.setStreamHandler(flutterPluginEventTest);
    }

    @Override
    public void onListen(Object o, final EventChannel.EventSink eventSink) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        eventSink.success(System.currentTimeMillis());
                    } catch (InterruptedException e) {
                        eventSink.error("error","error",e.getMessage());
                    }
                }
            }
        }).start();

    }

    @Override
    public void onCancel(Object o) {
        Log.e("Event信息:", "onCancel: "+o);
    }
}
